<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <div class="layui-row">
      <div class="layui-col-md6 select-depot-storage">
        <div class="filter-wrapper">
          <NewPageFilter
            v-model:query="filterInfo.leftQuery.queryMap"
            :query-more-json="true"
            :filter-list="filterInfo.searchList"
            :filter-btn="filterInfo.searchBtn"
          ></NewPageFilter>
        </div>
        <div class="filter-wrapper flex-end">
          <NewPageFilter :filter-btn="filterInfo.leftBtnList">
            <template v-slot:filter-btn>
              <el-button
                :disabled="
                  !leftTableInfo.selections || !leftTableInfo.selections.length
                "
                @click="removeRight"
                >右移</el-button
              >
            </template>
          </NewPageFilter>
        </div>
        <el-form :model="leftTableInfo" style="width: 100%">
          <dialog-table
            ref="leftFromTableInfoRef"
            v-model:data="leftTableInfo.data"
            :pager="false"
            :tab-index="false"
            :check-box="true"
            check-box-fixed="right"
            :field-list="leftTableInfo.fieldList"
            :highlight-row="false"
            :selectable="selectable"
            :row-class-name="leftTableRowClassName"
            @handleEvent="leftHandleEvent"
          >
            <template v-slot:col-price="scope">
              <span>{{ $fn.TripartiteMethod(scope.row.price) }}</span>
            </template>
          </dialog-table>
        </el-form>
      </div>
      <div
        class="layui-col-md6 select-depot-storage"
        style="padding-left: 20px"
      >
        <div class="filter-wrapper flex-end">
          <NewPageFilter :filter-btn="filterInfo.leftBtnList">
            <template v-slot:filter-btn>
              <div class="scan-groups scan-input-box">
                <ScanInputBox
                  :query="scanInfoQuery"
                  :api="scanInfo.api"
                  style="margin-right: 10px"
                  @callback="onCallBackScanData"
                />
              </div>
            </template>
          </NewPageFilter>
        </div>
        <div class="filter-wrapper">
          <NewPageFilter :filter-btn="filterInfo.leftBtnList">
            <template v-slot:filter-btn>
              <el-button
                :disabled="
                  !tableInfo.selections || !tableInfo.selections.length
                "
                @click="removeLeft"
                >左移</el-button
              >
              <el-button-group style="margin-left: 10px">
                <el-button @click="changeNumberAll('receive')"
                  >一键填充</el-button
                >
                <el-button @click="changeNumberAll('notOneKey')"
                  >一键清空</el-button
                >
              </el-button-group>
              <el-button-group style="margin-left: 10px">
                <el-button @click="changeNumberAll('receiveAll')"
                  >全部收货</el-button
                >
                <el-button @click="changeNumberAll('refuseAll')"
                  >全部拒收</el-button
                >
              </el-button-group>
            </template>
          </NewPageFilter>
        </div>
        <el-form ref="tableFormRef" :model="tableInfo" style="width: 100%">
          <dialog-table
            ref="formTableInfoRef"
            :refresh="tableInfo.refresh"
            :init-curpage="tableInfo.initCurpage"
            v-model:data="tableInfo.data"
            :pager="false"
            :tab-index="false"
            :check-box="true"
            :field-list="tableInfo.fieldList"
            :highlight-row="false"
            :summary="true"
            :summary-field-list="tableInfo.summaryFieldList"
            :row-class-name="tableRowClassName"
            :handle="tableInfo.handle"
            @handleEvent="rightHandleEvent"
          >
            <template v-slot:col-qty="scope">
              <el-form-item class="margin-bottom">
                <div
                  v-if="scope.row.codeType === 1 || scope.row.codeType === 4"
                  class="page-table-input-number-wrapper"
                >
                  <div class="page-table-input-number-left">
                    <div class="page-table-input-number-item">
                      <el-form-item
                        :prop="`data.${scope.$index}.largeUnitQty`"
                        :rules="
                          scope.$index >= 0
                            ? [
                                {
                                  validator: $validate.positiveAndZero,
                                  trigger: 'blur',
                                },
                              ]
                            : []
                        "
                      >
                        <el-input-number
                          v-model.lazy="scope.row.largeUnitQty"
                          style="width: 100%; margintop: 18px"
                          clearable
                          controls-position="right"
                          :precision="
                            scope.row.qtyOperateMode === 0 ||
                            scope.row.qtyOperateMode === 2
                              ? 0
                              : systemConfig.qty_show_precision
                          "
                          :min="0"
                          :max="largeMax(scope.row)"
                          @change="
                            changeLarge(
                              $event,
                              scope.row,
                              `data.${scope.$index}.largeUnitQty`
                            )
                          "
                        />
                      </el-form-item>
                      <div class="page-table-input-number-item-unite">
                        {{ scope.row.unitName }}
                      </div>
                    </div>
                    <div
                      v-if="
                        scope.row.unitConvertCoefficient > 1 &&
                        scope.row.qtyOperateMode === 2
                      "
                      class="page-table-input-number-item"
                    >
                      <el-form-item>
                        <el-input-number
                          v-model.lazy="scope.row.smallUnitQty"
                          style="width: 100%; margintop: 18px"
                          clearable
                          controls-position="right"
                          :precision="0"
                          :min="0"
                          :max="smallMax(scope.row)"
                        />
                      </el-form-item>
                      <div class="page-table-input-number-item-unite">
                        {{ scope.row.calcUnitName }}
                      </div>
                    </div>
                  </div>
                  <div
                    v-if="scope.row.qtyOperateMode === 2"
                    class="page-table-input-number-right"
                  >
                    {{ $fn.qtyFormatter(scope.row.qty) }}
                  </div>
                </div>
                <template
                  v-else-if="
                    scope.row.codeType === 2 || scope.row.codeType === 3
                  "
                >
                  <span class="qty-show">
                    {{ $fn.qtyFormatter(scope.row.qty) }}
                  </span>
                </template>
              </el-form-item>
            </template>
            <template v-slot:col-refuseQty="scope">
              <el-form-item class="margin-bottom">
                <div
                  v-if="scope.row.codeType === 1 || scope.row.codeType === 4"
                  class="page-table-input-number-wrapper"
                >
                  <div class="page-table-input-number-left">
                    <div class="page-table-input-number-item">
                      <el-form-item
                        :prop="`data.${scope.$index}.refuseLargeUnitQty`"
                        :rules="
                          scope.$index >= 0
                            ? [
                                {
                                  validator: $validate.positiveAndZero,
                                  trigger: 'blur',
                                },
                              ]
                            : []
                        "
                      >
                        <el-input-number
                          v-model.lazy="scope.row.refuseLargeUnitQty"
                          style="width: 100%; margintop: 18px"
                          clearable
                          controls-position="right"
                          :precision="
                            scope.row.qtyOperateMode === 0 ||
                            scope.row.qtyOperateMode === 2
                              ? 0
                              : systemConfig.qty_show_precision
                          "
                          :min="0"
                          :max="refuseLargeMax(scope.row)"
                          @change="
                            refuseChangeLarge(
                              $event,
                              scope.row,
                              `data.${scope.$index}.refuseLargeUnitQty`
                            )
                          "
                        />
                      </el-form-item>
                      <div class="page-table-input-number-item-unite">
                        {{ scope.row.unitName }}
                      </div>
                    </div>
                    <div
                      v-if="
                        scope.row.unitConvertCoefficient > 1 &&
                        scope.row.qtyOperateMode === 2
                      "
                      class="page-table-input-number-item"
                    >
                      <el-form-item>
                        <el-input-number
                          v-model.lazy="scope.row.refuseSmallUnitQty"
                          style="width: 100%; margintop: 18px"
                          clearable
                          controls-position="right"
                          :precision="0"
                          :min="0"
                          :max="refuseSmallMax(scope.row)"
                        />
                      </el-form-item>
                      <div class="page-table-input-number-item-unite">
                        {{ scope.row.calcUnitName }}
                      </div>
                    </div>
                  </div>
                  <div
                    v-if="scope.row.qtyOperateMode === 2"
                    class="page-table-input-number-right"
                  >
                    {{ $fn.qtyFormatter(scope.row.refuseQty) }}
                  </div>
                </div>
                <template
                  v-else-if="
                    scope.row.codeType === 2 || scope.row.codeType === 3
                  "
                >
                  <span class="qty-show">
                    {{ $fn.qtyFormatter(scope.row.refuseQty) }}
                  </span>
                </template>
              </el-form-item>
            </template>
            <template v-slot:col-tem="scope">
              <el-input-number
                v-model="scope.row.tem"
                controls-position="right"
                style="width: 100px"
                :min="-100"
                :max="100"
                :step="1"
              />
            </template>
            <template v-slot:col-hum="scope">
              <el-input-number
                v-model="scope.row.hum"
                controls-position="right"
                style="width: 100px"
                :min="0"
                :max="100"
                :precision="0"
                :step="1"
              />
            </template>
            <template v-slot:col-price="scope">
              <el-form-item
                v-if="!groupList.includes('price')"
                :prop="`data.${scope.$index}.price`"
                :rules="
                  scope.$index >= 0
                    ? [
                        {
                          validator: $validate.positiveMoneyZero,
                          trigger: 'blur',
                        },
                      ]
                    : []
                "
              >
                <el-input-number
                  v-model="scope.row.price"
                  controls-position="right"
                  style="width: 100%; margintop: 18px"
                  :min="0"
                  :max="999999999999999"
                  :precision="systemConfig.price_precision"
                  :step="1"
                  @change="
                    changMoney($event, scope.row, `data.${scope.$index}.price`)
                  "
                />
              </el-form-item>
              <span v-else>{{ $fn.TripartiteMethod(scope.row.price) }}</span>
              <p
                style="color: red"
                v-if="
                  scope.row.isLimitPrice == 1 &&
                  scope.row.price > scope.row.purchaseLimitPrice
                "
              >
                超过产品限价:{{ scope.row.purchaseLimitPrice }}
              </p>
            </template>
            <template v-slot:col-packageMarkValue="scope">
              <select
                v-model.lazy="scope.row.packageMark"
                @change="changeSelectAll($event, 'package_mark', scope.row)"
                class="select"
              >
                <option
                  v-for="(item, index) in $store.state.permission.dict[
                    'package_mark'
                  ]"
                  :key="index"
                  :value="item.code"
                >
                  {{ item.name }}
                </option>
              </select>
            </template>
            <template v-slot:col-identifyMarkValue="scope">
              <select
                v-model.lazy="scope.row.identifyMark"
                @change="changeSelectAll($event, 'identify_mark', scope.row)"
                class="select"
              >
                <option
                  v-for="(item, index) in $store.state.permission.dict[
                    'identify_mark'
                  ]"
                  :key="index"
                  :value="item.code"
                >
                  {{ item.name }}
                </option>
              </select>
            </template>
            <template v-slot:col-certificateMarkValue="scope">
              <select
                v-model.lazy="scope.row.certificateMark"
                @change="changeSelectAll($event, 'certificate_mark', scope.row)"
                class="select"
              >
                <option
                  v-for="(item, index) in $store.state.permission.dict[
                    'certificate_mark'
                  ]"
                  :key="index"
                  :value="item.code"
                >
                  {{ item.name }}
                </option>
              </select>
            </template>
            <template v-slot:col-appearanceQualityValue="scope">
              <select
                v-model.lazy="scope.row.appearanceQuality"
                @change="
                  changeSelectAll($event, 'appearance_quality', scope.row)
                "
                class="select"
              >
                <option
                  v-for="(item, index) in $store.state.permission.dict[
                    'appearance_quality'
                  ]"
                  :key="index"
                  :value="item.code"
                >
                  {{ item.name }}
                </option>
              </select>
            </template>
            <template v-slot:col-invalidReasonValue="scope">
              <select
                v-model.lazy="scope.row.invalidReason"
                @change="changeSelectAll($event, 'invalid_reason', scope.row)"
                class="select"
              >
                <option
                  v-for="(item, index) in $store.state.permission.dict[
                    'invalid_reason'
                  ]"
                  :key="index"
                  :value="item.code"
                >
                  {{ item.name }}
                </option>
              </select>
            </template>
            <template v-slot:col-rejectionReason="scope">
              <el-input
                type="input"
                clearable
                v-model="scope.row.rejectionReason"
              >
              </el-input>
            </template>
            <template v-slot:bt-switch="scope">
              <el-switch
                v-if="
                  scope.data.row.codeType === 2 || scope.data.row.codeType === 3
                "
                v-model="scope.data.row.receiveFlag"
                inactive-text="拒"
                active-text="收"
                @change="changeQtySwitch($event, scope.data.row)"
              ></el-switch>
            </template>
          </dialog-table>
        </el-form>
      </div>
    </div>
  </page-dialog>
</template>

<script setup name="SingleProduct">
import PageDialog from '@/components/PageDialog/index'
import DialogTable from '@/components/DialogTable/index'

import {
  receivingDetailsCustomizeListAll,
  getReceivingIndividualByDeliver,
  getReceivingsScans,
} from '@/api/stock'
import { getCustomizeSetting } from '@/api/customize'
import ScanInputBox from './ScanInputBox'
//个体操作
const { proxy } = getCurrentInstance()
const emit = defineEmits()
const props = defineProps([
  'showDialog',
  'selectObj',
  'isSysGroup',
  'query',
  'info',
  'settings',
  'deliverId',
  'orderId',
  'tableData',
  'scanInfoQuery',
  'fillData',
  'groupList',
  'queryParams',
  'leftQuery',
  'leftCustomizeUserVO',
  'leftDetailIds',
  'isEstimationStockBusiness',
])

const systemConfig = ref(proxy.$store.state.permission.systemConfig)
const scanInfo = reactive({
  api: getReceivingsScans,
})
const filterInfo = reactive({
  query: {
    customizeUserVO: {},
    queryMap: {
      ...props.query,
      deliverDetailId: {
        queryType: 'NOT_IN',
        value: null,
      },
    },
    queryParams: props.queryParams,
  },
  leftQuery: {
    customizeUserVO: props.leftCustomizeUserVO,
    queryMap: {
      ...props.leftQuery,
      id: {
        queryType: 'NOT_IN',
        value: null,
      },
    },
    queryParams: props.queryParams,
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
  leftBtnList: [{ type: 'slot', value: 'btn' }],
})
const formInfo = reactive({
  ref: null,
  data: {},
  rules: {},
})
const leftTableInfo = ref({
  data: [],
  copyData: [],
  loading: true,
  fieldList: [],
  selections: [],
  detailIds: '',
})
// 表格相关
const tableInfo = ref({
  summaryFieldList: [
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'refuseQty', formatter: proxy.$fn.qtyFormatter },
    { value: 'deliverQty', formatter: proxy.$fn.qtyFormatter },
    { value: 'pendReceiveQty', formatter: proxy.$fn.qtyFormatter },
    { value: 'price', show: 'false' },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  selections: [],
  refresh: 1,
  initCurpage: 1,
  data: [],
  mergeDetail: {},
  loading: true,
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '开关',
        name: 'switch',
        slot: true,
      },
    ],
  },
})
// 弹窗相关
const dialogInfo = reactive({
  title: '个体操作',
  btLoading: false,
  width: '90%',
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      event: save,
      show: true,
    },
  ],
})
const tableFormRef = ref()
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emit('update:showDialog', newValue) // 更新父组件shows
  },
})
watch(
  () => tableInfo.value.data,
  (val) => {
    if (val && val.length) {
      val.forEach((row) => {
        let smallUnitQty = row.smallUnitQty || 0
        let refuseSmallUnitQty = row.refuseSmallUnitQty || 0
        let unitConvertCoefficient = row.unitConvertCoefficient || 1
        let largeUnitQty = row.largeUnitQty || 0
        let refuseLargeUnitQty = row.refuseLargeUnitQty || 0
        let calcQty = proxy.$fn.accAdd(
          proxy.$fn.accMul(largeUnitQty, unitConvertCoefficient),
          smallUnitQty
        )
        let refuseCalcQty = proxy.$fn.accAdd(
          proxy.$fn.accMul(refuseLargeUnitQty, unitConvertCoefficient),
          refuseSmallUnitQty
        )
        let qty = proxy.$fn.accDiv(calcQty, unitConvertCoefficient)
        let refuseQty = proxy.$fn.accDiv(refuseCalcQty, unitConvertCoefficient)
        row['calcQty'] = calcQty
        row['qty'] = qty
        row['refuseCalcQty'] = refuseCalcQty
        row['refuseQty'] = refuseQty
        row.sumAmount = proxy.$fn.accDiv(
          proxy.$fn.accMul(calcQty, row.price),
          unitConvertCoefficient
        )
        if (systemConfig.value.is_estimation_stock_business == '1') {
          if (props.isEstimationStockBusiness == '1') {
            row.isEstimationStockBusiness = '1'
            row.isEstimationStockBusinessValue = '是'
          } else if (props.isEstimationStockBusiness == '0') {
            row.isEstimationStockBusiness = '0'
            row.isEstimationStockBusinessValue = '否'
          }
        }
      })
    }
  },
  {
    deep: true,
  }
)
watch(
  () => leftTableInfo.value.data,
  (val) => {
    if (val && val.length) {
      val.forEach((row) => {
        if (systemConfig.value.is_estimation_stock_business == '1') {
          if (props.isEstimationStockBusiness == '1') {
            row.isEstimationStockBusiness = '1'
            row.isEstimationStockBusinessValue = '是'
          } else if (props.isEstimationStockBusiness == '0') {
            row.isEstimationStockBusiness = '0'
            row.isEstimationStockBusinessValue = '否'
          }
        }
      })
    }
  },
  {
    deep: true,
  }
)
let obj = {
  price: {
    label: '单价',
    value: 'price',
    minWidth: '150px',
    type: 'slot',
    componentType: 'price',
    permission: !proxy.$permission('receiving:price'),
    how: false,
    hidden: true,
  },
  sumAmount: {
    label: '总金额',
    value: 'sumAmount',
    type: 'column',
    componentType: 'price',
    formatter: proxy.$fn.TripartiteMethod,
    permission: !proxy.$permission('receiving:price'),
    how: false,
    hidden: true,
  },
  defaultSupplierPrice: {
    label: '默认供应商价格',
    value: 'defaultSupplierPrice',
    componentType: 'price',
    type: 'column',
    permission: !proxy.$permission('receiving:price'),
    formatter: proxy.$fn.TripartiteMethod,
    how: false,
    hidden: true,
  },
  defaultPrice: {
    label: '默认价格',
    value: 'defaultPrice',
    type: 'column',
    componentType: 'price',
    permission: !proxy.$permission('receiving:price'),
    formatter: proxy.$fn.TripartiteMethod,
    hidden: true,
    how: false,
  },
  commonName: {
    label: '通用名称',
    value: 'commonName',
    componentType: 'input',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
  qty: {
    label: '数量',
    value: 'qty',
    type: 'slot',
    minWidth: '150px',
    how: false,
  },
  refuseQty: {
    label: '拒收数',
    value: 'refuseQty',
    type: 'slot',
    minWidth: '150px',
    how: false,
  },
  tem: {
    label: '到货温度',
    value: 'tem',
    type: 'slot',
    componentType: 'number',
    how: false,
    hidden: true,
  },
  hum: {
    label: '到货湿度',
    type: 'slot',
    componentType: 'number',
    value: 'hum',
    how: false,
    hidden: true,
  },
  packageMarkValue: {
    label: '包装标识',
    value: 'packageMarkValue',
    componentType: 'number',
    type: 'slot',
    show: false,
    hidden: true,
  },
  identifyMarkValue: {
    label: '确定标识',
    value: 'identifyMarkValue',
    type: 'slot',
    show: false,
    hidden: true,
  },
  certificateMarkValue: {
    label: '合格证',
    value: 'certificateMarkValue',
    type: 'slot',
    show: false,
    hidden: true,
  },
  appearanceQualityValue: {
    label: '外观质量',
    value: 'appearanceQualityValue',
    type: 'slot',
    show: false,
    hidden: true,
  },
  invalidReasonValue: {
    label: '不合格原因值',
    value: 'invalidReasonValue',
    type: 'slot',
    show: false,
    hidden: true,
  },
  rejectionReason: {
    label: '拒收原因',
    value: 'rejectionReason',
    type: 'slot',
    show: false,
    hidden: true,
  },
})
leftTableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
filterInfo.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
tableInfo.value.mergeDetail = props.info
leftTableInfo.value.detailIds = props.leftDetailIds
getCustomize()
getLeftCustomize()
function changeSelectAll(e, str, row) {
  let val = e.target.value
  if (!val) {
    return
  }
  let obj = {
    package_mark: 'packageMark',
    identify_mark: 'identifyMark',
    certificate_mark: 'certificateMark',
    appearance_quality: 'appearanceQuality',
    invalid_reason: 'invalidReason',
  }
  let option = proxy.$store.state.permission.dict[str]
  let optionItem = option.find((item) => item.code === val)
  row[obj[str] + 'Value'] = optionItem.name
}
function changMoney(val, row, str) {
  tableFormRef.value.clearValidate(str)
  tableInfo.value.data.forEach((item) => {
    if (item.productId === row.productId) {
      item.price = val
    }
  })
  tableInfo.value.mergeDetail.price = val
  tableInfo.value.mergeDetail.changePrice = true
}
function changeNumberAll(type) {
  tableInfo.value.data.forEach((item) => {
    let unitConvertCoefficient = item.unitConvertCoefficient || 1
    if (type === 'receive') {
      if (item.codeType === 4 || item.codeType === 1) {
        let num = proxy.$fn.Subtr(item.pendReceiveCalcQty, item.refuseCalcQty)
        if (num > 0) {
          item.calcQty = num
          if (item.qtyOperateMode === 2) {
            item.largeUnitQty = parseInt(item.calcQty / unitConvertCoefficient)
            item.smallUnitQty = parseInt(item.calcQty % unitConvertCoefficient)
          } else {
            item.largeUnitQty = item.calcQty
            item.smallUnitQty = 0
          }
        }
      } else if (item.codeType === 2 || item.codeType === 3) {
        if (item.qty === 0 && item.refuseQty === 0) {
          if (item.qtyOperateMode === 2) {
            let largeUnitQty = parseInt(
              item.pendReceiveCalcQty / unitConvertCoefficient
            )
            let smallUnitQty = parseInt(
              item.pendReceiveCalcQty % unitConvertCoefficient
            )
            if (item.receiveFlag) {
              item.largeUnitQty = largeUnitQty
              item.smallUnitQty = smallUnitQty
            } else {
              item.refuseLargeUnitQty = largeUnitQty
              item.refuseSmallUnitQty = smallUnitQty
            }
          } else {
            if (item.receiveFlag) {
              item.largeUnitQty = item.pendReceiveQty
              item.smallUnitQty = 0
            } else {
              item.refuseLargeUnitQty = item.pendReceiveQty
              item.refuseSmallUnitQty = 0
            }
          }
        }
      }
    } else if (type === 'notOneKey') {
      item.largeUnitQty = 0
      item.smallUnitQty = 0
      item.refuseLargeUnitQty = 0
      item.refuseSmallUnitQty = 0
    } else if (type === 'receiveAll') {
      item.calcQty = item.pendReceiveCalcQty
      item.largeUnitQty = parseInt(item.calcQty / unitConvertCoefficient)
      item.smallUnitQty = parseInt(item.calcQty % unitConvertCoefficient)
      if (item.codeType === 2 || item.codeType === 3) {
        item.receiveFlag = true
      }
      item.refuseLargeUnitQty = 0
      item.refuseSmallUnitQty = 0
    } else if (type === 'refuseAll') {
      item.refuseCalcQty = item.pendReceiveCalcQty
      item.refuseLargeUnitQty = parseInt(
        item.refuseCalcQty / unitConvertCoefficient
      )
      item.refuseSmallUnitQty = parseInt(
        item.refuseCalcQty % unitConvertCoefficient
      )
      if (item.codeType === 2 || item.codeType === 3) {
        item.receiveFlag = false
      }
      item.largeUnitQty = 0
      item.smallUnitQty = 0
    }
  })
}
// 数量操作模式 qtyOperateMode：0整数模式，2自动算出小数，1手动输入小数
// 条码类型 code_type：1无码，2系统唯一码，3原厂码，4批次码
function changeLarge(e, row, str) {
  tableFormRef.value.clearValidate(str)
  let largeUnitQty = parseInt(
    row.pendReceiveCalcQty / row.unitConvertCoefficient
  )
  if (row.qtyOperateMode === 2 && row.unitConvertCoefficient > 1) {
    if (largeUnitQty > 0 && e === largeUnitQty) {
      let smallUnitQty = parseInt(
        row.pendReceiveCalcQty % row.unitConvertCoefficient
      )
      if (row.smallUnitQty > smallUnitQty) {
        row.smallUnitQty = smallUnitQty
      }
    }
  }
}
function largeMax(row) {
  let unitConvertCoefficient = row.unitConvertCoefficient || 1
  if (row.qtyOperateMode === 2) {
    return parseInt(row.pendReceiveCalcQty / unitConvertCoefficient)
  } else {
    return row.pendReceiveQty
  }
}
function smallMax(row) {
  let unitConvertCoefficient = row.unitConvertCoefficient || 1
  let largeUnitQty = parseInt(row.pendReceiveCalcQty / unitConvertCoefficient)
  if (largeUnitQty === 0 || row.largeUnitQty === largeUnitQty) {
    return parseInt(row.pendReceiveCalcQty % unitConvertCoefficient)
  } else {
    return unitConvertCoefficient - 1
  }
}
function refuseChangeLarge(e, row, str) {
  tableFormRef.value.clearValidate(str)
  let refuseLargeUnitQty = parseInt(
    row.pendReceiveCalcQty / row.unitConvertCoefficient
  )

  if (row.qtyOperateMode === 2 && row.unitConvertCoefficient > 1) {
    if (refuseLargeUnitQty > 0 && e === refuseLargeUnitQty) {
      let refuseSmallUnitQty = parseInt(
        row.pendReceiveCalcQty % row.unitConvertCoefficient
      )
      if (row.refuseSmallUnitQty > refuseSmallUnitQty) {
        row.refuseSmallUnitQty = refuseSmallUnitQty
      }
    }
  }
}
function refuseLargeMax(row) {
  let unitConvertCoefficient = row.unitConvertCoefficient || 1
  if (row.qtyOperateMode === 2) {
    return parseInt(row.pendReceiveCalcQty / unitConvertCoefficient)
  } else {
    return row.pendReceiveQty
  }
}
function refuseSmallMax(row) {
  let unitConvertCoefficient = row.unitConvertCoefficient || 1
  let refuseLargeUnitQty = parseInt(
    row.pendReceiveCalcQty / unitConvertCoefficient
  )
  if (
    refuseLargeUnitQty === 0 ||
    row.refuseLargeUnitQty === refuseLargeUnitQty
  ) {
    return parseInt(row.pendReceiveCalcQty % unitConvertCoefficient)
  } else {
    return unitConvertCoefficient - 1
  }
}
function changeQtySwitch(e, row) {
  let unitConvertCoefficient = row.unitConvertCoefficient || 1
  if (e) {
    if (row.qtyOperateMode === 2) {
      row.largeUnitQty = parseInt(
        row.pendReceiveCalcQty / unitConvertCoefficient
      )
      row.smallUnitQty = parseInt(
        row.pendReceiveCalcQty % unitConvertCoefficient
      )
    } else {
      row.largeUnitQty = row.pendReceiveCalcQty
      row.smallUnitQty = 0
    }

    row.refuseLargeUnitQty = 0
    row.refuseSmallUnitQty = 0
  } else {
    if (row.qtyOperateMode === 2) {
      row.refuseLargeUnitQty = parseInt(
        row.pendReceiveCalcQty / unitConvertCoefficient
      )
      row.refuseSmallUnitQty = parseInt(
        row.pendReceiveCalcQty % unitConvertCoefficient
      )
    } else {
      row.refuseLargeUnitQty = row.pendReceiveCalcQty
      row.refuseSmallUnitQty = 0
    }
    row.largeUnitQty = 0
    row.smallUnitQty = 0
  }
}
function search() {
  getLeftList()
}
function leftHandleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      leftTableInfo.value.selections = data
      break
  }
}
function rightHandleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = data
      break
  }
}
function removeLeft() {
  //右边往左移
  let selections = tableInfo.value.selections
  let queryMap = filterInfo.leftQuery.queryMap
  let rowKey = selections.map((item) => {
    let key = generateProductKey(item)
    return key
  })
  let deliverDetailIdS = selections.map((row) => row.deliverDetailId)
  let ids = proxy.$fn.splitArr(queryMap.id.value)
  if (ids.length) {
    ids = ids.map((item) => !deliverDetailIdS.includes(Number(item)))
    queryMap.id.value = ids.length ? ids.toString() : null
  }
  tableInfo.value.data = tableInfo.value.data.filter(
    (item) => !rowKey.includes(generateProductKey(item))
  )
  let item = leftTableInfo.value.copyData.filter((item) =>
    rowKey.includes(generateProductKey(item))
  )
  if (item.length) {
    if (item.length === selections.length) {
      leftTableInfo.value.data = item.concat(leftTableInfo.value.data)
    } else {
      let keys = item.map((item) => {
        let key = generateProductKey(item)
        return key
      })
      let itemList = selections.filter(
        (item) => !keys.includes(generateProductKey(item))
      )
      itemList.forEach((row) => {
        row.price = row.defaultPrice
        row.hum = 0
        row.tem = 0
        row.packageMark = 1
        row.packageMarkValue = '完整'
        row.identifyMark = 1
        row.identifyMarkValue = '是'
        row.certificateMark = 1
        row.certificateMarkValue = '有'
        row.appearanceQuality = 1
        row.appearanceQualityValue = '正常'
        row.invalidReason = 1
        row.invalidReasonValue = '是'
        row.largeUnitQty = 0
        row.smallUnitQty = 0
        row.refuseLargeUnitQty = 0
        row.refuseSmallUnitQty = 0
        row.refuseCalcQty = 0
        row.calcQty = 0
        row.qty = 0
        row.refuseQty = 0
        row.sumAmount = 0
        if (row.codeType === 1 || row.codeType === 4) {
          row.receiveFlag = false
          row.refuseFlag = false
        }
      })
      leftTableInfo.value.data = item.concat(leftTableInfo.value.data)
      leftTableInfo.value.data = itemList.concat(leftTableInfo.value.data)
    }
  } else {
    selections.forEach((row) => {
      row.price = row.defaultPrice
      row.hum = 0
      row.tem = 0
      row.packageMark = 1
      row.packageMarkValue = '完整'
      row.identifyMark = 1
      row.identifyMarkValue = '是'
      row.certificateMark = 1
      row.certificateMarkValue = '有'
      row.appearanceQuality = 1
      row.appearanceQualityValue = '正常'
      row.invalidReason = 1
      row.invalidReasonValue = '是'
      row.largeUnitQty = 0
      row.smallUnitQty = 0
      row.refuseLargeUnitQty = 0
      row.refuseSmallUnitQty = 0
      row.refuseCalcQty = 0
      row.calcQty = 0
      row.sumAmount = 0
      row.qty = 0
      row.refuseQty = 0
      if (row.codeType === 1 || row.codeType === 4) {
        row.receiveFlag = false
        row.refuseFlag = false
      }
    })
    leftTableInfo.value.data = selections.concat(leftTableInfo.value.data)
    leftTableInfo.value.detailIds = leftTableInfo.value.data
      .map((row) => row.deliverDetailId)
      .join(',')
    proxy.$refs['formTableInfoRef'].clearSelection()
  }
}
function removeRight() {
  //左边往右移
  let info = props.info
  let queryMap = filterInfo.leftQuery.queryMap
  let selections = leftTableInfo.value.selections
  let unitConvertCoefficient = selections[0].unitConvertCoefficient || 1
  let rowKey = selections.map((item) => {
    let key = generateProductKey(item)
    return key
  })
  let ids = proxy.$fn.splitArr(queryMap.id.value)
  let deliverDetailIdS = selections.map((row) => row.deliverDetailId)
  ids = ids.concat(deliverDetailIdS)
  queryMap.id.value = ids.length ? ids.toString() : null
  leftTableInfo.value.data = leftTableInfo.value.data.filter(
    (item) => !rowKey.includes(generateProductKey(item))
  )
  leftTableInfo.value.detailIds = leftTableInfo.value.data
    .map((row) => row.deliverDetailId)
    .join(',')
  selections.forEach((row) => {
    if (tableInfo.value.mergeDetail.changePrice) {
      row.price = info.price
    }
    if (props.settings.hum) {
      row.hum = props.info.hum
    }
    if (props.settings.tem) {
      row.tem = props.info.tem
    }
    if (props.settings.rejectionReason) {
      row.rejectionReason = props.info.rejectionReason
    }
    if (props.settings.packageMark) {
      row.packageMark = props.info.packageMark
      row.packageMarkValue = props.info.packageMarkValue
    }
    if (props.settings.identifyMark) {
      row.identifyMark = props.info.identifyMark
      row.identifyMarkValue = props.info.identifyMarkValue
    }
    if (props.settings.certificateMark) {
      row.certificateMark = props.info.certificateMark
      row.certificateMarkValue = props.info.certificateMarkValue
    }
    if (props.settings.appearanceQuality) {
      row.appearanceQuality = props.info.appearanceQuality
      row.appearanceQualityValue = props.info.appearanceQualityValue
    }
    if (props.settings.invalidReason) {
      row.invalidReason = props.info.invalidReason
      row.invalidReasonValue = props.info.invalidReasonValue
    }
    if (row.codeType === 1 || row.codeType === 4) {
      if (row.pendReceiveQty >= 1) {
        row.calcQty = unitConvertCoefficient
        if (row.qtyOperateMode === 2) {
          row.largeUnitQty = parseInt(row.calcQty / unitConvertCoefficient)
          row.smallUnitQty = parseInt(row.calcQty % unitConvertCoefficient)
        } else if (row.qtyOperateMode === 0 || row.qtyOperateMode === 1) {
          row.largeUnitQty = 1
          row.smallUnitQty = 0
        }
      } else {
        row.calcQty = row.pendReceiveCalcQty
        if (row.qtyOperateMode === 2) {
          row.largeUnitQty = parseInt(row.calcQty / unitConvertCoefficient)
          row.smallUnitQty = parseInt(row.calcQty % unitConvertCoefficient)
        } else if (row.qtyOperateMode === 0 || row.qtyOperateMode === 1) {
          row.largeUnitQty = row.pendReceiveQty
          row.smallUnitQty = 0
        }
      }
    } else if (row.codeType === 2 || row.codeType === 3) {
      row.receiveFlag = true
      row.calcQty = row.pendReceiveCalcQty
      if (row.pendReceiveQty >= 1) {
        row.largeUnitQty = 1
        row.smallUnitQty = 0
      } else {
        if (row.qtyOperateMode === 2) {
          row.largeUnitQty = 0
          row.smallUnitQty = row.calcQty
        } else if (row.qtyOperateMode === 0 || row.qtyOperateMode === 1) {
          row.largeUnitQty = row.pendReceiveQty
          row.smallUnitQty = 0
        }
      }
    }
  })
  tableInfo.value.data = selections.concat(tableInfo.value.data)
  proxy.$refs['leftFromTableInfoRef'].clearSelection()
}
function getList() {
  let query = filterInfo.query
  query.queryMap.deliverDetailId.value = leftTableInfo.value.detailIds
  receivingDetailsCustomizeListAll(query).then((res) => {
    if (res && res.success) {
      let data = res.data
      let info = props.info
      if (data && data.length > 0) {
        let unitConvertCoefficient = data[0].unitConvertCoefficient || 1
        data.forEach((item) => {
          if (props.settings.hum) {
            item.hum = info.hum
          }
          if (props.settings.tem) {
            item.tem = info.tem
          }
          if (props.settings.rejectionReason) {
            item.rejectionReason = info.rejectionReason
          }
          if (props.settings.packageMark) {
            item.packageMark = info.packageMark
            item.packageMarkValue = info.packageMarkValue
          }
          if (props.settings.identifyMark) {
            item.identifyMark = info.identifyMark
            item.identifyMarkValue = info.identifyMarkValue
          }
          if (props.settings.certificateMark) {
            item.certificateMark = info.certificateMark
            item.certificateMarkValue = info.certificateMarkValue
          }
          if (props.settings.appearanceQuality) {
            item.appearanceQuality = info.appearanceQuality
            item.appearanceQualityValue = info.appearanceQualityValue
          }
          if (props.settings.invalidReason) {
            item.invalidReason = info.invalidReason
            item.invalidReasonValue = info.invalidReasonValue
          }
          if (props.info.fill === 'receive') {
            if (item.codeType === 4 || item.codeType === 1) {
              let num = proxy.$fn.Subtr(
                item.pendReceiveCalcQty,
                item.refuseCalcQty
              )
              if (num > 0) {
                item.calcQty = num
                if (item.qtyOperateMode === 2) {
                  item.largeUnitQty = parseInt(
                    item.calcQty / unitConvertCoefficient
                  )
                  item.smallUnitQty = parseInt(
                    item.calcQty % unitConvertCoefficient
                  )
                } else {
                  item.largeUnitQty = item.calcQty
                  item.smallUnitQty = 0
                }
              }
            } else if (item.codeType === 2 || item.codeType === 3) {
              if (item.qty === 0 && item.refuseQty === 0) {
                item.receiveFlag = true
                if (item.qtyOperateMode === 2) {
                  item.largeUnitQty = parseInt(
                    item.pendReceiveCalcQty / unitConvertCoefficient
                  )
                  item.smallUnitQty = parseInt(
                    item.pendReceiveCalcQty % unitConvertCoefficient
                  )
                } else {
                  item.largeUnitQty = item.pendReceiveQty
                  item.smallUnitQty = 0
                }
              }
            }
          } else if (props.info.fill === 'refuse') {
            if (item.codeType === 4 || item.codeType === 1) {
              let num = proxy.$fn.Subtr(item.pendReceiveCalcQty, item.calcQty)
              if (num > 0) {
                item.refuseCalcQty = num
                if (item.qtyOperateMode === 2) {
                  item.refuseLargeUnitQty = parseInt(
                    item.refuseCalcQty / unitConvertCoefficient
                  )
                  item.refuseSmallUnitQty = parseInt(
                    item.refuseCalcQty % unitConvertCoefficient
                  )
                } else {
                  item.refuseLargeUnitQty = item.calcQty
                  item.refuseSmallUnitQty = 0
                }
              }
            } else if (item.codeType === 2 || item.codeType === 3) {
              if (item.qty === 0 && item.refuseQty === 0) {
                item.receiveFlag = false
                if (item.qtyOperateMode === 2) {
                  item.refuseLargeUnitQty = parseInt(
                    item.pendReceiveCalcQty / unitConvertCoefficient
                  )
                  item.refuseSmallUnitQty = parseInt(
                    item.pendReceiveCalcQty % unitConvertCoefficient
                  )
                } else {
                  item.refuseLargeUnitQty = item.pendReceiveQty
                  item.refuseSmallUnitQty = 0
                }
              }
            }
          }
        })
        tableInfo.value.data = data
        filterInfo.leftQuery.queryMap.id.value = data
          .map((item) => item.deliverDetailId)
          .toString()
      }
    }
    getLeftList()
  })
}
function onCallBackScanData(obj) {
  let { mergeDetail, detail } = obj
  if (props.settings.hum) {
    detail.hum = props.info.hum
    mergeDetail.hum = props.info.hum
  }
  if (props.settings.tem) {
    detail.tem = props.info.tem
    mergeDetail.tem = props.info.tem
  }
  if (props.settings.rejectionReason) {
    detail.rejectionReason = props.info.rejectionReason
    mergeDetail.rejectionReason = props.info.rejectionReason
  }
  if (props.settings.packageMark) {
    detail.packageMark = props.info.packageMark
    detail.packageMarkValue = props.info.packageMarkValue
    mergeDetail.packageMark = props.info.packageMark
    mergeDetail.packageMarkValue = props.info.packageMarkValue
  }
  if (props.settings.identifyMark) {
    detail.identifyMark = props.info.identifyMark
    detail.identifyMarkValue = props.info.identifyMarkValue
    mergeDetail.identifyMark = props.info.identifyMark
    mergeDetail.identifyMarkValue = props.info.identifyMarkValue
  }
  if (props.settings.certificateMark) {
    detail.certificateMark = props.info.certificateMark
    mergeDetail.certificateMarkValue = props.info.certificateMarkValue
    mergeDetail.certificateMark = props.info.certificateMark
    detail.certificateMarkValue = props.info.certificateMarkValue
  }
  if (props.settings.appearanceQuality) {
    detail.appearanceQuality = props.info.appearanceQuality
    detail.appearanceQualityValue = props.info.appearanceQualityValue
    mergeDetail.appearanceQuality = props.info.appearanceQuality
    mergeDetail.appearanceQualityValue = props.info.appearanceQualityValue
  }
  if (props.settings.invalidReason) {
    detail.invalidReason = props.info.invalidReason
    mergeDetail.invalidReason = props.info.invalidReason
    detail.invalidReasonValue = props.info.invalidReasonValue
    mergeDetail.invalidReasonValue = props.info.invalidReasonValue
  }
  if (tableInfo.value.mergeDetail.changePrice) {
    mergeDetail.price = props.info.price
    detail.price = props.info.price
  }
  tableInfo.value.data.forEach((item) => {
    item.isChoice = false
  })
  let key = ''
  for (let item in props.selectObj) {
    key = key + mergeDetail[item] + '&'
  }
  if (key === props.info.key) {
    let index = tableInfo.value.data.findIndex((row) => {
      let rowKey = generateProductKey(row)
      return rowKey === generateProductKey(detail)
    })
    if (index !== -1) {
      let newItem = tableInfo.value.data[index]
      newItem.isChoice = true
      if (newItem.codeType === 4) {
        if (
          proxy.$fn.accAdd(newItem.calcQty, newItem.refuseCalcQty) >=
          newItem.pendReceiveCalcQty
        ) {
          proxy.$message({
            message: `当前产品已处理完`,
            type: 'success',
            duration: 3500,
            showClose: true,
          })
        } else {
          let num = proxy.$fn.Subtr(
            newItem.pendReceiveCalcQty,
            proxy.$fn.accAdd(newItem.calcQty, newItem.refuseCalcQty)
          )
          let unitConvertCoefficient = newItem.unitConvertCoefficient || 1
          if (num >= unitConvertCoefficient) {
            newItem.calcQty = proxy.$fn.accAdd(
              unitConvertCoefficient,
              newItem.calcQty
            )
          } else {
            newItem.calcQty = proxy.$fn.accAdd(num, newItem.calcQty)
          }
          if (newItem.qtyOperateMode === 2) {
            newItem.largeUnitQty = parseInt(
              newItem.calcQty / newItem.unitConvertCoefficient
            )
            newItem.smallUnitQty = parseInt(
              newItem.calcQty % newItem.unitConvertCoefficient
            )
          } else if (
            newItem.qtyOperateMode === 0 ||
            newItem.qtyOperateMode === 1
          ) {
            newItem.largeUnitQty = newItem.calcQty
            newItem.smallUnitQty = 0
          }
          tableInfo.value.data[index] = newItem
          successInfo()
        }
      } else if (newItem.codeType === 2 || newItem.codeType === 3) {
        if (newItem.qty === 0 && newItem.refuseQty === 0) {
          if (newItem.qtyOperateMode === 2) {
            let largeUnitQty = parseInt(
              newItem.pendReceiveCalcQty / newItem.unitConvertCoefficient
            )
            let smallUnitQty = parseInt(
              newItem.pendReceiveCalcQty % newItem.unitConvertCoefficient
            )
            if (newItem.receiveFlag) {
              newItem.largeUnitQty = largeUnitQty
              newItem.smallUnitQty = smallUnitQty
            } else {
              newItem.refuseLargeUnitQty = largeUnitQty
              newItem.refuseSmallUnitQty = smallUnitQty
            }
          } else {
            if (newItem.receiveFlag) {
              newItem.largeUnitQty = newItem.pendReceiveQty
              newItem.smallUnitQty = 0
            } else {
              newItem.refuseLargeUnitQty = newItem.pendReceiveQty
              newItem.refuseSmallUnitQty = 0
            }
          }
          successInfo()
        } else {
          proxy.$message({
            message: `当前产品已处理完`,
            type: 'success',
            duration: 3500,
            showClose: true,
          })
        }
      }
    } else {
      tableInfo.value.data.unshift(detail)
    }
    leftTableInfo.value.data = leftTableInfo.value.data.filter(
      (item) => generateProductKey(item) !== generateProductKey(detail)
    )
  } else {
    proxy.$message({
      message: `扫码的产品不在当前弹窗内，请重新再试`,
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
  }
}
function successInfo() {
  proxy.$message({
    message: `扫码成功`,
    type: 'success',
    duration: 3500,
    showClose: true,
  })
}
function generateProductKey(item) {
  let key =
    item.productId +
    '&' +
    item.batchNumber +
    '&' +
    item.validityDate +
    '&' +
    item.productionDate +
    '&' +
    item.defaultPrice +
    '&' +
    item.qrCode +
    '&' +
    item.uniqueCode +
    '&' +
    item.factorySubCode +
    '&' +
    item.factoryMainCode
  return key
}
function handleClose() {
  shows.value = false
}
function save() {
  proxy.$refs['tableFormRef'].validate((valid) => {
    if (valid) {
      let largeUnitQty = 0
      let refuseLargeUnitQty = 0
      let smallUnitQty = 0
      let refuseSmallUnitQty = 0
      let sumAmount = 0
      let unitConvertCoefficient = props.info.unitConvertCoefficient || 1
      tableInfo.value.data.forEach((item) => {
        largeUnitQty = proxy.$fn.accAdd(largeUnitQty, item.largeUnitQty)
        smallUnitQty = proxy.$fn.accAdd(smallUnitQty, item.smallUnitQty)
        refuseLargeUnitQty = proxy.$fn.accAdd(
          refuseLargeUnitQty,
          item.refuseLargeUnitQty
        )
        refuseSmallUnitQty = proxy.$fn.accAdd(
          refuseSmallUnitQty,
          item.refuseSmallUnitQty
        )
        sumAmount = proxy.$fn.accAdd(sumAmount, item.sumAmount)
      })
      let calcQty = proxy.$fn.accAdd(
        proxy.$fn.accMul(largeUnitQty, unitConvertCoefficient),
        smallUnitQty
      )
      let refuseCalcQty = proxy.$fn.accAdd(
        proxy.$fn.accMul(refuseLargeUnitQty, unitConvertCoefficient),
        refuseSmallUnitQty
      )
      tableInfo.value.mergeDetail.sumAmount = sumAmount
      tableInfo.value.mergeDetail.calcQty = calcQty
      tableInfo.value.mergeDetail.refuseCalcQty = refuseCalcQty
      tableInfo.value.mergeDetail.qty = proxy.$fn.accDiv(
        calcQty,
        unitConvertCoefficient
      )
      tableInfo.value.mergeDetail.leftDetailIds = leftTableInfo.value.detailIds
      tableInfo.value.mergeDetail.refuseQty = proxy.$fn.accDiv(
        refuseCalcQty,
        unitConvertCoefficient
      )
      let details = tableInfo.value.data.filter(
        (item) => item.qty > 0 || item.refuseQty > 0
      )
      if (details.length) {
        let itemList = details.filter(
          (item) =>
            proxy.$fn.accAdd(item.calcQty || 0, item.refuseCalcQty) >
            item.pendReceiveCalcQty
        )
        if (itemList.length > 0) {
          proxy.$alert(
            `操作的个体的数量与拒收数量之和不能大于待收货数量`,
            '操作提示',
            {
              confirmButtonText: '确定',
              dangerouslyUseHTMLString: true,
            }
          )
          return
        }
        let changeTem = changeVal(details, 'tem')
        let changeHum = changeVal(details, 'hum')
        let changeRejectionReason = changeVal(details, 'rejectionReason')
        let changePackageMark = changeVal(details, 'packageMark')
        let changeIdentifyMark = changeVal(details, 'identifyMark')
        let changeCertificateMark = changeVal(details, 'certificateMark')
        let changeAppearanceQuality = changeVal(details, 'appearanceQuality')
        let changeInvalidReason = changeVal(details, 'invalidReason')
        tableInfo.value.mergeDetail.tem = changeTem
          ? details[0].tem
          : tableInfo.value.mergeDetail.tem
        tableInfo.value.mergeDetail.hum = changeHum
          ? details[0].hum
          : tableInfo.value.mergeDetail.hum
        tableInfo.value.mergeDetail.rejectionReason = changeRejectionReason
          ? details[0].rejectionReason
          : tableInfo.value.mergeDetail.rejectionReason
        tableInfo.value.mergeDetail.packageMark = changePackageMark
          ? details[0].packageMark
          : tableInfo.value.mergeDetail.packageMark
        tableInfo.value.mergeDetail.packageMarkValue = changePackageMark
          ? details[0].packageMarkValue
          : tableInfo.value.mergeDetail.packageMarkValue
        tableInfo.value.mergeDetail.identifyMark = changeIdentifyMark
          ? details[0].identifyMark
          : tableInfo.value.mergeDetail.identifyMark
        tableInfo.value.mergeDetail.identifyMarkValue = changeIdentifyMark
          ? details[0].identifyMarkValue
          : tableInfo.value.mergeDetail.identifyMarkValue
        tableInfo.value.mergeDetail.certificateMark = changeCertificateMark
          ? details[0].certificateMark
          : tableInfo.value.mergeDetail.certificateMark
        tableInfo.value.mergeDetail.certificateMarkValue = changeCertificateMark
          ? details[0].certificateMarkValue
          : tableInfo.value.mergeDetail.certificateMarkValue
        tableInfo.value.mergeDetail.appearanceQuality = changeAppearanceQuality
          ? details[0].appearanceQuality
          : tableInfo.value.mergeDetail.appearanceQuality
        tableInfo.value.mergeDetail.appearanceQualityValue =
          changeAppearanceQuality
            ? details[0].appearanceQualityValue
            : tableInfo.value.mergeDetail.appearanceQualityValue
        tableInfo.value.mergeDetail.invalidReason = changeInvalidReason
          ? details[0].invalidReason
          : tableInfo.value.mergeDetail.invalidReason
        tableInfo.value.mergeDetail.invalidReasonValue = changeInvalidReason
          ? details[0].invalidReasonValue
          : tableInfo.value.mergeDetail.invalidReasonValue
        tableInfo.value.mergeDetail.groupConcatIds = details
          .map((item) => item.groupConcatIds)
          .toString()
      } else {
        tableInfo.value.mergeDetail.groupConcatIds = ''
      }
      tableInfo.value.mergeDetail.details = details
      tableInfo.value.mergeDetail.fill = 'notOneKey'
      handleClose()
      emit('callback', tableInfo.value.mergeDetail)
    }
  })
}
function changeVal(data, str) {
  let val = data[0][str]
  let arr = data.filter((item) => item[str] === val)
  return arr.length === data.length
}
function tableRowClassName({ row }) {
  if (
    proxy.$fn.accAdd(row.calcQty, row.refuseCalcQty) > row.pendReceiveCalcQty ||
    (proxy.$store.state.permission.systemConfig
      .receiving_verify_different_supplier_price == 1 &&
      row.defaultPrice !== row.defaultSupplierPrice)
  ) {
    return 'warning-row'
  }
  if (row.isChoice) {
    return 'success-row'
  }
  return ''
}
function leftTableRowClassName({ row }) {
  if (
    (row.codeType === 2 || row.codeType === 3) &&
    row.pendReceiveCalcQty === 0
  ) {
    return 'grey-row'
  }
}
function getCustomize() {
  let param = {
    tableKey:
      'stockReceivingOrderSingleModify-com.slip.spd.entity.vo.stock.ReceivingOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      filterInfo.query.customizeUserVO = data
      if (props.info.details && props.info.details.length) {
        tableInfo.value.data = props.info.details
        if (props.orderId) {
          filterInfo.leftQuery.queryMap.id.value = props.info.details
            .map((item) => item.deliverDetailId)
            .toString()
          getLeftList()
        }
      } else if (props.orderId) {
        if (props.info.groupConcatIds) {
          getList()
        } else {
          getLeftList()
        }
      }
    }
  })
}
function getLeftList() {
  let info = props.info
  getReceivingIndividualByDeliver(filterInfo.leftQuery).then((res) => {
    leftTableInfo.value.copyData = proxy.$fn.deepClone(res.data)
    let data = res.data.filter((item) => item.pendReceiveCalcQty > 0)
    let otherData = res.data.filter((item) => item.pendReceiveCalcQty <= 0)
    let unitConvertCoefficient = info.unitConvertCoefficient || 1
    if (info.fill === 'receive' && data.length) {
      data.forEach((item) => {
        if (props.settings.hum) {
          item.hum = props.info.hum
        }
        if (props.settings.tem) {
          item.tem = props.info.tem
        }
        if (props.settings.rejectionReason) {
          item.rejectionReason = props.info.rejectionReason
        }
        if (props.settings.packageMark) {
          item.packageMark = props.info.packageMark
          item.packageMarkValue = props.info.packageMarkValue
        }
        if (props.settings.identifyMark) {
          item.identifyMark = props.info.identifyMark
          item.identifyMarkValue = props.info.identifyMarkValue
        }
        if (props.settings.certificateMark) {
          item.certificateMark = props.info.certificateMark
          item.certificateMarkValue = props.info.certificateMarkValue
        }
        if (props.settings.appearanceQuality) {
          item.appearanceQuality = props.info.appearanceQuality
          item.appearanceQualityValue = props.info.appearanceQualityValue
        }
        if (props.settings.invalidReason) {
          item.invalidReason = props.info.invalidReason
          item.invalidReasonValue = props.info.invalidReasonValue
        }
        if (item.codeType === 4 || item.codeType === 1) {
          let num = proxy.$fn.Subtr(item.pendReceiveCalcQty, item.refuseCalcQty)
          if (num > 0) {
            item.calcQty = num
            if (item.qtyOperateMode === 2) {
              item.largeUnitQty = parseInt(
                item.calcQty / unitConvertCoefficient
              )
              item.smallUnitQty = parseInt(
                item.calcQty % unitConvertCoefficient
              )
            } else {
              item.largeUnitQty = item.calcQty
              item.smallUnitQty = 0
            }
          }
        } else if (item.codeType === 2 || item.codeType === 3) {
          if (item.qty === 0 && item.refuseQty === 0) {
            item.receiveFlag = true
            if (item.qtyOperateMode === 2) {
              item.largeUnitQty = parseInt(
                item.pendReceiveCalcQty / unitConvertCoefficient
              )
              item.smallUnitQty = parseInt(
                item.pendReceiveCalcQty % unitConvertCoefficient
              )
            } else {
              item.largeUnitQty = item.pendReceiveQty
              item.smallUnitQty = 0
            }
          }
        }
      })
      tableInfo.value.data = data.concat(tableInfo.value.data)
      leftTableInfo.value.data = otherData
      return
    } else if (info.fill === 'refuse' && data.length) {
      data.forEach((item) => {
        if (props.settings.hum) {
          item.hum = props.info.hum
        }
        if (props.settings.tem) {
          item.tem = props.info.tem
        }
        if (props.settings.rejectionReason) {
          item.rejectionReason = props.info.rejectionReason
        }
        if (props.settings.packageMark) {
          item.packageMark = props.info.packageMark
          item.packageMarkValue = props.info.packageMarkValue
        }
        if (props.settings.identifyMark) {
          item.identifyMark = props.info.identifyMark
          item.identifyMarkValue = props.info.identifyMarkValue
        }
        if (props.settings.certificateMark) {
          item.certificateMark = props.info.certificateMark
          item.certificateMarkValue = props.info.certificateMarkValue
        }
        if (props.settings.appearanceQuality) {
          item.appearanceQuality = props.info.appearanceQuality
          item.appearanceQualityValue = props.info.appearanceQualityValue
        }
        if (props.settings.invalidReason) {
          item.invalidReason = props.info.invalidReason
          item.invalidReasonValue = props.info.invalidReasonValue
        }
        if (item.codeType === 4 || item.codeType === 1) {
          let num = proxy.$fn.Subtr(item.pendReceiveCalcQty, item.calcQty)
          if (num > 0) {
            item.refuseCalcQty = num
            if (item.qtyOperateMode === 2) {
              item.refuseLargeUnitQty = parseInt(
                item.refuseCalcQty / unitConvertCoefficient
              )
              item.refuseSmallUnitQty = parseInt(
                item.refuseCalcQty % unitConvertCoefficient
              )
            } else {
              item.refuseLargeUnitQty = item.refuseCalcQty
              item.refuseSmallUnitQty = 0
            }
          }
        } else if (item.codeType === 2 || item.codeType === 3) {
          if (item.qty === 0 && item.refuseQty === 0) {
            item.receiveFlag = false
            if (item.qtyOperateMode === 2) {
              item.refuseLargeUnitQty = parseInt(
                item.pendReceiveCalcQty / unitConvertCoefficient
              )
              item.refuseSmallUnitQty = parseInt(
                item.pendReceiveCalcQty % unitConvertCoefficient
              )
            } else {
              item.refuseLargeUnitQty = item.pendReceiveQty
              item.refuseSmallUnitQty = 0
            }
          }
        }
      })
      tableInfo.value.data = data.concat(tableInfo.value.data)
      leftTableInfo.value.data = otherData
      return
    }
    leftTableInfo.value.data = res.data
    leftTableInfo.value.detailIds = res.data
      .map((row) => row.deliverDetailId)
      .join(',')
  })
}
function getLeftCustomize() {
  let param = {
    tableKey:
      'stockReceivingOrderSingleModifyLeft-com.slip.spd.entity.vo.stock.ReceivingOrderDetailVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      leftTableInfo.value.fieldList = proxy.$fn.setFieldList(
        leftTableInfo.value.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.searchList,
        data.detail
      )
      filterInfo.searchList = searchInfo.searchList
      filterInfo.searchList.forEach((item) => {
        item.event = search
      })
      filterInfo.leftQuery.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.leftQuery.queryMap,
      }
      if (props.deliverId) {
        if (props.info.details && props.info.details.length) {
          filterInfo.leftQuery.queryMap.id.value = props.info.details
            .map((item) => item.deliverDetailId)
            .toString()
        }
        getLeftList()
      }
    }
  })
}
function selectable(row) {
  if (row.codeType === 1 || row.codeType === 4) {
    return true
  } else if (row.codeType === 2 || row.codeType === 3) {
    if (!(row.pendReceiveCalcQty > 0)) {
      row.disabled = true
    }
    return row.pendReceiveCalcQty > 0
  }
}
</script>

<style scoped lang="scss">
.select {
  width: 100%;
  height: 28px;
  padding: 0 15px;
  box-sizing: border-box;
  border-radius: 4px;
  color: #606266;
  border: 1px solid #dcdfe6;
}

.qty-show {
  display: inline-block;
  width: 100%;
}
</style>
